package com.auctionhaus



class Listing {

    String listName
    Date endDateTime
    Double startBidPrice
    Double maxBidPrice
    Double nextBidPrice
    //Set bids

    static hasMany = [bids:Bid]
    String description
    Customer seller
    Customer winner

    static belongsTo = [seller:Customer]

    static transients = [ 'maxBidPrice', 'nextBidPrice']

    static constraints = {
      description(nullable:true, size:0..256)
      listName(size:1..64)
      winner(nullable:true)
      startBidPrice(nullable:false )
      endDateTime(nullable:false, validator: { val, obj -> obj.properties['endDateTime'] >= new Date()
                })
      bids(nullable:true, blank:true)
      winner(nullable:true)


    }

    static mapping = {
        seller updateable: false
        winner updateable: false
    }
     Double getNextBidPrice() {
           getMaxBidPrice() + Bid.incrementRate
     }
    Double getMaxBidPrice() {
           if (this.bids?.size() > 0)
                this.bids.collect{bid -> bid.amount}.max()
           else
                startBidPrice
    }



}
